<?php

namespace app\api\model;

use think\Model;

class Adv extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 广告圈列表
     * @param $user_id
     * @param $city string 城市
     * @param $search string 标题
     * @return \think\Paginator
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */

    public function advList($user_id, $city = null, $search = null)
    {
        $where = [];
        if($city){
            $cityWhere['a.city'] = ['eq', $city];
        }else{
            $cityWhere = 'a.city = b.city';
        }
        if($search){
            $where['a.title'] = ['like','%'.$search.'%'];
        }
        $data = $this
            ->alias('a')
            ->where('a.status', 1)
            ->join('user_info b', 'a.user_id = b.user_id')
            ->where($where)
            ->where($cityWhere)
            ->order('a.total desc')
            ->field("a.id,a.user_id,a.title,a.content,a.price,a.city,a.images,b.avatar,b.birthday,b.username,a.gender,b.status,FROM_UNIXTIME(a.createtime,'%Y-%m-%d %H:%i:%s') as createtime")
            ->paginate(10);
        $userInfo = model('user_info')->where('user_id', $user_id)->field('id,city,gender,birthday')->find();
//        $userInfo['user_age'] = (int)$date - (int)substr($userInfo['birthday'], 0, 4);//年龄
        foreach ($data as $k => $v) {
            $data[$k]['user_age'] = user_age($v['birthday']);//年龄
            $data[$k]['members'] = model('members')->where('user_id', $user_id)->value('status') ?? 0;//会员等级
            $data[$k]['comment_num'] = model('adv_comment')->where('adv_id', $v['id'])->count();//评论数量
            $data[$k]['like_num'] = model('adv_like')->where('adv_id', $v['id'])->count();//点赞数量
            $data[$k]['gold_coin'] = model('user_wallet')
                ->where(['user_id' => $user_id, 'type' => 4, 'adv_id' => $v['id']])
                ->value('gold_coin')?1:0;//获取金币
            $data[$k]['attention'] = attention($user_id, $v['user_id']);
            $data[$k]['distance'] = '12km';
            $data[$k]['like_status'] = $this->name('adv_like')->where(['user_id' => $user_id, 'adv_id' => $v['id']])->find() ? 1 : 0;//判断是否已点赞
        }
        return $data;
    }

    public function advDetail($user_id, $adv_id)
    {
        $data = self::getById($adv_id);
        $userinfo = model('user_info')->where('user_id', $data['user_id'])
            ->field('username,avatar,status as auth_status,birthday')->find();
        $data['username'] = $userinfo['username'];
        $data['avatar'] = $userinfo['avatar'];
        $data['auth_status'] = $userinfo['auth_status'];
        $data['createtime'] = date('Y-m-d H:i:s', $data['createtime']);
        $data['user_age'] = user_age($userinfo['birthday']);//年龄
        $data['members'] = model('members')->where('user_id', $data['user_id'])->value('status') ?? 0;//会员等级
        $data['like_status'] = $this->name('adv_like')->where(['user_id' => $user_id, 'adv_id' => $adv_id])->find() ? 1 : 0;//判断是否已点赞
        $data['attention'] = attention($user_id, $data['user_id']);
        //评论列表
        $comment_list = $this->name('adv_comment')
            ->alias('a')
            ->join('user_info b', 'a.user_id = b.user_id')
            ->where(['adv_id' => $adv_id])
            ->field("b.username,b.avatar,FROM_UNIXTIME(a.createtime,'%Y-%m-%d %H:%i:%s') as createtime,a.content,a.id,a.user_id,a.reply_id")
            ->select();
        foreach ($comment_list as $key => $val) {
            //回复列表
            $comment_list[$key]['reply'] = model('adv_reply')
                ->alias('a')
                ->join('user_info b', 'a.user_id = b.user_id')
                ->join('user_info c', 'a.reply_id = c.user_id')
                ->where('comment_id', $val['id'])
                ->field('b.username,c.username as reply_username,a.content,a.user_id')
                ->select();
//            if($val['reply_id']){
//                $comment_list[$key]['reply_name'] = model('user_info')->where('user_id', $val['reply_id'])->value('username');
//            }
        }
        $data['comment_list'] = $comment_list;//评论列表
        $data['comment_num'] = count($comment_list);//评论数量
        $data['like_num'] = model('adv_like')->where('adv_id', $adv_id)->count();//点赞数量
        $data['gold_coin'] = model('user_wallet')
            ->where(['user_id' => $user_id, 'type' => 4, 'adv_id' => $adv_id])
            ->value('gold_coin')?1:0;//获取金币
        $data['distance'] = '12km';
        return $data;
    }
}